package tool

import (
	"beergo/ext/util/tool"
	"github.com/beego/beego/v2/server/web"
	"io/ioutil"
	"net/http"
	"os"
)

const WeatherFileDir = "/var/tmp"
const WeatherFilePath = "/var/tmp/weather.json"
var root string

func init() {
	root = web.AppConfig.DefaultString("Root","")
}

func GetWeather() {
	url := "http://wthrcdn.etouch.cn/weather_mini?citykey=101121201"
	resp,err:=http.Get(url)
	if err != nil {
		println("天气信息获取失败1")
		println(err.Error())
		return
	}
	if resp == nil {
		println("天气信息获取失败2")
		println("内容为空")
		return
	}
	var content []byte
	content,err=ioutil.ReadAll(resp.Body)
	if err != nil {
		println("天气信息读取失败3")
		println(err.Error())
		return
	}
	file := root + WeatherFilePath
	tool.CreateDirForPath(file)
	err = os.WriteFile(file,content,0777)
	if err != nil {
		println("天气信息读取失败5")
		return
	}
	println("天气信息 获取 ok")
}
